require(["common", "coAjax", "baiduMap"], function(jSouper, coAjax, baiduMap) {
	baiduMap(function() {
		var map = new BMap.Map("allmap");
		var ownLbs = App.get("bus_info.info.lbs_info");
		var localSearch = new BMap.LocalSearch(map);
		map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
		map.enableScrollWheelZoom(); //启用滚轮放大缩小，默认禁用
		map.enableContinuousZoom(); //启用地图惯性拖拽，默认禁用
		map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
		var mkr; //标注点容器
		var logostr; //logo路径
		if (App.get("bus_info.info.lgo_url")) {
			logostr = App.get("bus_info.info.lgo_url") + "?imageView/2/w/60";
		} else {
			logostr = "/images/logo.png"
		};
		var infoWindow = new BMap.InfoWindow("<img style='width:60px;height:60px;margin-right:5px;vertical-align:top;display:inline-block' src='" + logostr + "' id='imgDemo'/><span style='width:135px;display:inline-block;vertical-align:top;white-space: normal;word-spacing: normal;word-wrap:break-word;word-break:break-all;'>" + App.get("bus_info.info.address") + "</span>", {
			width: 200, // 信息窗口宽度
			title: App.get("bus_info.info.title"), // 信息窗口标题
			enableMessage: true, //设置允许信息窗发送短息
			message: "这个店铺不错哦，一起分享吧！"
		}); //信息窗口容器

		// 加载地图
		if (ownLbs) {
			// 如果已有地理标注
			lbsPoint = new BMap.Point(ownLbs.lng, ownLbs.lat);
			map.centerAndZoom(lbsPoint, 13);
			mkr = new BMap.Marker(lbsPoint, {
				enableDragging: true,
				raiseOnDrag: true
			});
			map.clearOverlays();
			map.addOverlay(mkr);
			map.openInfoWindow(infoWindow, lbsPoint);
			// 标注点拖拽事件，关闭信息窗口
			mkr.addEventListener('dragend', function(e) {
				map.closeInfoWindow(infoWindow);
			})
		} else {
			// 没有地理标注
			var myCity = new BMap.LocalCity();
			myCity.get(function(result) {
				showMap(result.name || "北京");
			});
		};

		// 地图拖拽改变标注位置
		map.addEventListener("dragend", function() {
			var pt = map.getBounds().getCenter();
			mkr.point.lng = pt.lng;
			mkr.point.lat = pt.lat;
		});


		// 加载地图及搜索通用
		function showMap(cityName) {
			localSearch.setSearchCompleteCallback(function(searchResult) {
				var poi = searchResult.getPoi(0);
				if (!poi) {
					alert("warn", "抱歉，您提供的内容暂时查找不到任何信息！！");
					return;
				}
				var center = new BMap.Point(poi.point.lng, poi.point.lat);
				map.centerAndZoom(center, 13);
				map.clearOverlays();
				mkr = new BMap.Marker(center, {
					enableDragging: true,
					raiseOnDrag: true
				});
				map.addOverlay(mkr);
				// 添加标注点拖动事件
				mkr.addEventListener('dragend', function(e) {
					// 关闭信息窗口
					map.closeInfoWindow(infoWindow);
				})
			})
			localSearch.search(cityName);
		}

		// logo加载完重绘信息框，防止图片加载之前显示信息窗口高度过小
		$("#imgDemo").onload = function() {
			infoWindow.redraw();
		}

		App.set("$Event.changCity", function() {
			showMap(App.get('$Cache.location'));
		});

		App.set("$Event.updateMap", function() {
			coAjax.put(appConfig.bus.set_baidu_lbs, {
				lat: mkr.point.lat,
				lon: mkr.point.lng
			}, function(r) {
				console.log(r.result);
				alert("success", "您的位置已标注成功！！！");
			}, function(e) {
				alert("error", "系统错误，请您稍后再试，抱歉");
			});
		})
	});

})